/**
 * JavaScript main file
 */

var layout;
var zIndex = 1;
var x = 20;
var y = 40;


$.sifr({
    path: '/fonts/',
    save: true


});
/**
 * Executing while document is loaded
 */
$(document).ready(function(){

    //page layout
    layout = $("body").layout({
        defaults:{
            fxName      :   "none",
            initClosed  :   true
        },
        north:{
            initClosed  :   true
        },
        south:{
            initClosed  :   true
        },
        west:{
            initClosed  :   false
        },
        east:{
            initClosed  :   true
        }
    });

    //    //show note form
    $("#createNoteBtn").click(function(){
        $("#noteForm").dialog({
            title: 'Dodaj notke',
            modal:  true,
            resizable: false,
            close: function(){
                $(this).dialog('destroy');
            }

        });

        $("#fontPreview input").each(function(){
            var fontName = $(this).val();
            $(this).prev('label').sifr({
                font: fontName,
                fontSize: '20px',
                cursor: 'pointer'
            });
        });
        
    });

    // submit note form
    $("#submitNoteBtn").click(function(){
        if($("#noteForm textarea").val() == ""){
            $(".formError").html("Musisz podać treść!");
            return false;
        }


        var data = $("#noteForm form").serialize();
        data = data + '&pos_z=' + (++zIndex);


        data = data + '&pos_y='+ y;
        data = data + '&pos_x='+ x;
        data = data + '&tab_id='+ $("#noteTabs li.ui-state-active").attr("id").replace("tab","");

        x+=40;
        y+=40;
        $.post(
            '/note/createNote',
            data,
            function(response){
                $("#pinboard").append(response);
                $("#noteForm textarea").val("");
                $("#noteForm").dialog('destroy');
            }
            );
      
    });

    //removing note
    $(".removeNote").live("click", function(){
        var note = $(this).closest(".note");

        $.post(
            '/note/delete',
            {
                id: note.attr("id").replace("note", "")
            },
            function(){
                note.fadeOut("fast", function(){
                    note.remove();
                }
                );

        
            });
    });

    //changing note z-index after click
    $(".note").live("mousedown", function(){
        if($(this).css('z-index') != (zIndex) ){
            $(this).css('z-index', ++zIndex);
            updateNotePosition($(this));
        }
    });

    //additional css
    css();

    //set z-index
    setZIndex();

});


/**
 * enable note dragging
 */
function makeNotesDraggable(selector) {
    $(selector).draggable({
        handle: ".head",
        start:function(){
        
        },
        stop:   function(){
            updateNotePosition($(this))
        },
        containment: 'parent',
        grid: [20,20]
    });
}


function css(){
    $(".ui-state-default").live("mouseover", function(){
        $(this).removeClass("ui-state-default").addClass("ui-state-hover");
    });

    $(".ui-state-hover").live("mouseout", function(){
        $(this).addClass("ui-state-default").removeClass("ui-state-hover");
    });
}

function setZIndex(){
    ///pobranie najwiekszego z index
    $(".note").each(function(){
        var index = parseInt($(this).css("z-index"));
        zIndex = Math.max(zIndex, index);
    });
}

function updateNotePosition(note){
    $.post(
        '/note/update',
        {
            id      :   note.attr("id").replace("note", ""),
            pos_x   :   note.position().left,
            pos_y   :   note.position().top,
            pos_z   :   zIndex
        }
        );
}